package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleResourceMapper roleResourceMapper;
    //分页角色
    @Override
    public PageResponse<RoleVo> findRolePage(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Role> rolePage = roleMapper.findRolePage(roleDto);
        PageResponse<RoleVo> pageResponse = PageResponse.of(rolePage, RoleVo.class);
        return pageResponse;
    }
    //角色添加
    @Override
    @Transactional(rollbackFor = Exception.class )
    public void createRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.createRole(role);
    }
    //根据id删除角色
    @Override
    @Transactional(rollbackFor = Exception.class )
    public void deleteRole(Long roleIds) {
        //删除role
        roleMapper.deleteRole(roleIds);
        //删除resource
        roleResourceMapper.deleteRoleResource(roleIds);
    }
    //根据角色id查询选中的资源
    @Override
    public Set<String> findCheckedResources(Long roleId) {
        Set<String> resourceNos = roleResourceMapper.findCheckedResources(roleId);
        return resourceNos;
    }
    //角色下拉框
    @Override
    public List<RoleVo> initRoles() {
        RoleDto build = new RoleDto();
        Page<Role> rolePage = roleMapper.selectPage(build);
        return BeanUtil.copyToList(rolePage.getResult(),RoleVo.class);
    }
    //角色修改
    @Override
    public Boolean updateRole(RoleDto roleDto) {
        //修改role
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.updateRole(role);
        //判断是否有资源
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())) {
            return true;
        }
        //删除role_resource
        roleResourceMapper.updateRoleResourceId(role.getId());
        //保存资源信息
        List<RoleResource> roleResourceList = Lists.newArrayList();
        //遍历信息
        Arrays.asList(roleDto.getCheckedResourceNos()).forEach(resourceNo -> {
            RoleResource roleResource = RoleResource.builder()
                    .roleId(role.getId())
                    .resourceNo(resourceNo)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            roleResourceList.add(roleResource);
        });
        //判断是否为空
        if (EmptyUtil.isNullOrEmpty(roleResourceList)){
            return true;
        }
        //批量保存
        roleResourceMapper.batchInsert(roleResourceList);
        return true;
    }

}
